<?php
/**
 * Created by PhpStorm.
 * Author: yostar
 * User: THINK
 * Date: 2018/5/31
 * Time: 10:50
 */

use wechat\model\AutoReply;
use wechat\model\Material;
use wechat\model\Menu;
use wechat\model\CustomerService;

require_once '../model/AutoReply.php';
require_once '../model/Material.php';
require_once '../model/Menu.php';
require_once '../model/CustomerService.php';

$index = new Index();
// 程序执行入口
if (isset($_GET['echostr'])) {
    $index->wechatObj->valid();
} else {
    $index->responseMsg();
//    var_dump($index->menu->get_menu());
//    var_dump($index->wechatObj->get_access_token());die;
    /*$data = [
        'kf_account' => 'yostar@gh_d32acc1d744b',
        'nickname' => 'yostar',
    ];
    $data = json_encode($data);
    $index->customer->addAccount($data);*/
//    var_dump($index->customer->getList());
//    $materialUrl = 'D:\resource\videoExample.mp4';
    /*$materialUrl = 'D:\resource\cxx2.jpg';
    var_dump($index->material->uploadPermanentMaterial($materialUrl,'image'));*/
//    $index->material->dd($index->material->getMaterialList('image', 0, 20));
//    $index->material->dd($index->material->getMaterialCount());
//    $index->material->dd($index->material->getMaterial('5SYct_yfPx3TVDuMJMUBafIrdm0vSnsdUjZMwYPJXzk', 'image'));
//    $index->material->dd($index->material->deleteMaterial('5SYct_yfPx3TVDuMJMUBafIrdm0vSnsdUjZMwYPJXzk'));

}

class Index
{
    public $wechatObj; // 微信对象
    public $material;  // 素材对象
    public $menu;      // 自定义菜单对象
    public $customer;  // 客服对象

    public function __construct()
    {
        $this->wechatObj = new AutoReply('wechat', 'false');
        $this->material = new Material();
        $this->menu = new Menu();
        $this->customer = new CustomerService();
    }

    /**
     * 消息回复总函数
     */
    public function responseMsg()
    {
        $this->wechatObj->log('in response');
        // 请求一次微信用户发送的数据
        $this->wechatObj->getMessage();
        // 获取到的数据 array数组
        $msg = $this->wechatObj->msg;
        if (!empty($msg)) {
            $this->wechatObj->log('msg is not empty: ' . var_export($msg, true));
            // 消息类型
            $msgType = $this->wechatObj->msgType;
            // 如果是文本消息
            if ($msgType == 'text') {
                $this->wechatObj->log('current message type is text');
                $keyword = $msg['Content'];
                $this->wechatObj->log('user sent the content is: ' . $keyword);
                switch (strtolower($keyword)) {
                    case '?':
                    case '？':
                        $this->wechatObj->log('come in switch case ?');
                        $result = $this->wechatObj->textMessage(date("Y-m-d H:i:s", time()) . '');
                        $this->wechatObj->log("the data send to user: \n" . $result);
                        echo $result;
                        break;
                    case 'hello':
                        $this->wechatObj->log('come in switch case hello');
                        $info = $this->wechatObj->getBaseUserInfo($this->wechatObj->msg['FromUserName']);
                        $result = $this->wechatObj->textMessage('你好,' . $info['nickname']);
                        $this->wechatObj->log("the data send to user:\n " . $result);
                        echo $result;
                        break;
                    case '图文':
                        $this->wechatObj->log('come in switch case 图文');
                        $result = $this->wechatObj->articleMessage([
                            [
                                'title' => '图文测试标题',
                                'description' => '图文测试内容',
                                'picUrl' => 'http://mmbiz.qpic.cn/mmbiz_jpg/7yrzZiawibDgnpLtz60SpsiahiaSsglnHno5CIQ8Fg6aTjlPzzWAjoGHVXc7BKHStibE8Wroc7OUGvIaOe1V3iaWWogw/0?wx_fmt=jpeg',
                                'url' => 'https://baidu.com'
                            ]
                        ]);
                        $this->wechatObj->log("the data send to user:\n " . $result);
                        echo $result;
                        break;
                    case '音乐':
                        $this->wechatObj->log('come in switch case 音乐');
                        $result = $this->wechatObj->musicMessage('5SYct_yfPx3TVDuMJMUBaap8jyylCqIHPwCyIdacNZQ', 'Almost Lover', '超喜欢的一首歌', 'http://hao.haolingsheng.com/ring/000/986/d931f3e68884c56f1a56cf91c2548eea.mp3', 'https://wechat.yostar.xin/asset/music/Almost%20Lover.mp3');
                        $this->wechatObj->log("the data send to user:\n" . $result);
                        echo $result;
                        break;
                    case '图片':
                        $this->wechatObj->log('come in switch case 图片');
                        $result = $this->wechatObj->mediaMessage('5SYct_yfPx3TVDuMJMUBaYHu4CShppnVzFojOAMKTro');
                        $this->wechatObj->log("the data send to user:\n" . $result);
                        echo $result;
                        break;
                    case '语音':
                        $this->wechatObj->log('come in switch case 语音');
//                        $result = $this->wechatObj->mediaMessage('', 'voice');
                        $result = $this->wechatObj->textMessage('语音回复测试暂未开始');
                        $this->wechatObj->log("the data send to user:\n" . $result);
                        echo $result;
                        break;
                    case '视频':
                        $this->wechatObj->log('come in switch case 视频');
                        $result = $this->wechatObj->videoMessage('q21SkdkgF8vMMryKL9f_VhXlRrrJ_HoCo3DHKITwgb3hQIj-x31dbV4waddLnZmx');
                        $this->wechatObj->log("the data send to user:\n" . $result);
                        echo $result;
                        break;

                }
            } // 如果是事件
            else if ($msgType == 'event') {
                $this->wechatObj->log('进入事件');
                $event = $this->wechatObj->msg['Event'];
                switch (strtolower($event)) {
                    case 'subscribe':
                        $this->wechatObj->log('进入关注事件');
                        $info = $this->wechatObj->getBaseUserInfo($this->wechatObj->msg['FromUserName']);
                        $string = date("Y-m-d H:i:s", time()) . " 关注\n";
                        $string .=  $info['nickname'] ."，欢迎关注本公众号，你可以根据以下提示测试自动回复：\n";
                        $string .= "回复 \"hello\" 测试回复文本消息\n";
                        $string .= "回复 \"图文\" 测试回复图文消息\n";
                        $string .= "回复 \"图片\" 测试回复图片消息\n";
                        $string .= "回复 \"语音\" 测试回复语音消息\n";
                        $string .= "回复 \"视频\" 测试回复视频消息\n";
                        $string .= "回复 \"音乐\" 测试回复音乐消息\n";
                        echo $this->wechatObj->textMessage($string);
                        break;
                    case 'unsubscribe': // 取消关注
                        echo $this->wechatObj->textMessage(date("Y-m-d H:i:s", time()) . ' 取消关注');
                        break;
                    case 'click': // click 菜单被点击
                        $eventKey = $this->wechatObj->msg['EventKey'];
                        switch ($eventKey) {
                            case 'music':
                                echo $this->wechatObj->musicMessage('5SYct_yfPx3TVDuMJMUBaap8jyylCqIHPwCyIdacNZQ', 'Almost Lover', '超喜欢的一首歌', 'https://wechat.yostar.xin/asset/music/Almost%20Lover.mp3', 'https://wechat.yostar.xin/asset/music/Almost%20Lover.mp3');
                                break;
                        }
                        break;
                    case 'view': // 带有url的菜单被点击
                        break;
                    case 'scancode_push': // 扫码推
                        $result = $this->wechatObj->msg['ScanResult'];
                        echo $this->wechatObj->textMessage( '扫描结果：' . $result);
                        break;
                    case 'scancode_waitmsg': // 扫码推事件且弹出“消息接收中”提示框
                        $result = $this->wechatObj->msg['ScanResult'];
                        echo $this->wechatObj->textMessage('扫描结果：' . $result);
                        break;
                    case 'pic_sysphoto': // 弹出系统拍照发图
                        $count = $this->wechatObj->msg['Count'];
                        echo $this->wechatObj->textMessage('发送了 ' . $count . ' 张图片');
                        break;
                    case 'pic_photo_or_album': // 弹出拍照或者相册发图
                        $count = $this->wechatObj->msg['Count'];
                        echo $this->wechatObj->textMessage('发送了 ' . $count . ' 张图片');
                        break;
                    case 'pic_weixin': // 弹出微信相册发图器
                        $count = $this->wechatObj->msg['Count'];
                        echo $this->wechatObj->textMessage('发送了 ' . $count . ' 张图片');
                        break;
                    case 'location_select': // 弹出地理位置选择器
                        break;
                    case 'location': // 用户发送位置信息
                        $latitude = $this->wechatObj->msg['Latitude'];
                        $longitude = $this->wechatObj->msg['Longitude'];
                        $precision = $this->wechatObj->msg['Precision'];
                        echo $this->wechatObj->textMessage('您当前所在的纬度：' . $latitude. ' 经度：' . $longitude . ' 精度：' . $precision);
                        break;

                }
            }
        } else {
            $this->wechatObj->log('msg is empty');
            echo '';
            exit();
        }
    }
}